from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools.utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")
af = IAnalysisFactory.create();

showFigures = True
writeFigures = not showFigures

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 17)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names"""
aidaPath = "/home/marc-antoine/Documents/BGO/QFin/QueensData/queenstools"
aidaFileT = "ScintillationAnalysis_BGO_QFile"
#aidaFileQ = ["Na22_300K_201_PhotonReanalysis_NoTuples", "Na22_30K_201_PhotonReanalysis_NoTuples", "Na22_3400mK_201_PhotonReanalysis_NoTuples_old"]
aidaFileQ = ["Na22_300K_201_PhotonReanalysis_NoTuples", "Na22_30K_201_PhotonReanalysis_NoTuples", "Na22_3400mK_201_PhotonReanalysis_NoTuples"]

outPath = "/home/marc-antoine/Dropbox/BGO"



#afrQ = AidaFileReader(aidaPathQ, aidaFileQ)

tupPath = "AllComboFit_Amp_Tabs.txt"
#avEvtPath = "Reanalysis/Average pulse shapes/Variable binning/Special Bin, Average Pulse from First Photon on Ch0, Ch1, All Cuts"
avEvtPath = "Reanalysis/Average pulse shapes/Variable binning/Special Bin, Average Pulse from First Photon on Ch1, Ch0, All Cuts"
#avEvtPath = "Reanalysis/Average pulse shapes/Variable binning/Special Bin, Average Pulse from First Photon on Ch1, Ch0, All Events"
#avEvtPath = "Reanalysis/Average pulse shapes/Variable binning/Special Bin, Average Pulse from First Photon on Ch1, Ch0, Some Cuts"

temper = ["300","30","3.4"]
#numComp = ["2","1","3"]
labPosY = [0.305, 0.6, 0.895]
zoomMax = [0.8, 0.2, 0.2]
funcSup = [5, 350, 1500]

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""
avEvt = []
func = []
funcZoom = []
label = []
jpl = []
tup = GrabIObject(AidaFileReader(aidaPath, aidaFileT).tree, tupPath).getTuple()
row = [9, 10, 11]
for i in range(len(temper)):
	treeH = AidaFileReader(aidaPath, aidaFileQ[i]).tree
	hist = GrabIObject(treeH, avEvtPath).histo1D
	scFac = hist.maxBinHeight()
	hist.scale(1.0/scFac)
	tup.setRow(row[i])
	fParam = [tup.getDouble(16), tup.getDouble(18), tup.getDouble(20), tup.getDouble(22), tup.getDouble(24), tup.getDouble(26), tup.getDouble(28), tup.getDouble(30), tup.getDouble(32), tup.getDouble(34), tup.getDouble(36)]
	
	f=0
	if (i==0 or i==1):
		f = af.createFunctionFactory(af.createTreeFactory().create()).createFunctionFromScript("AvEvt Fit", 1, "A0*(exp(-(x[0]-t0)/tau0)-exp(-(x[0]-t0)/tauR))+A1*exp(-(x[0]-t0)/tau1)+A2*exp(-(x[0]-t0)/tau2)+B", "A0,tau0,A1,tau1,A2,tau2,B,t0,tauR", "AvEvt Fit")
		f.setParameter("A0", fParam[0]/scFac)
		f.setParameter("tau0", fParam[1])
		f.setParameter("A1", fParam[2]/scFac)
		f.setParameter("tau1", fParam[3])
		f.setParameter("A2", fParam[4]/scFac)
		f.setParameter("tau2", fParam[5])
		f.setParameter("t0", fParam[8])
		f.setParameter("tauR", fParam[9])
		f.setParameter("B", fParam[10]/scFac)
		
	elif (i==2):
		f = af.createFunctionFactory(af.createTreeFactory().create()).createFunctionFromScript("AvEvt Fit", 1, "A0*(exp(-(x[0]-t0)/tau0)-exp(-(x[0]-t0)/tauR))+A1*exp(-(x[0]-t0)/tau1)+A2*exp(-(x[0]-t0)/tau2)+A3*exp(-(x[0]-t0)/tau3)+B", "A0,tau0,A1,tau1,A2,tau2,A3,tau3,B,t0,tauR", "AvEvt Fit")
		f.setParameter("A0", fParam[0]/scFac)
		f.setParameter("tau0", fParam[1])
		f.setParameter("A1", fParam[2]/scFac)
		f.setParameter("tau1", fParam[3])
		f.setParameter("A2", fParam[4]/scFac)
		f.setParameter("tau2", fParam[5])
		f.setParameter("A3", fParam[6]/scFac)
		f.setParameter("tau3", fParam[7])
		f.setParameter("t0", fParam[8])
		f.setParameter("tauR", fParam[9])
		f.setParameter("B", fParam[10]/scFac)
	
	""" The Histograms """
	avEvt.append(H1D(hist, title=temper[i]+" K", style="h", color=colors[0], penWidth=2, errAll=False))
	
	""" The Functions """
	func.append(F1D(f, 0, funcSup[i], title=temper[i]+" K", points=1000, color=colors[1]))
	jpl.append(func[i].getDrawOption())
	jpl[i].penWidth=1.5
	func[i].setDrawOption(jpl[i])
	
	""" The Functions Zoom """
	funcZoom.append(F1D(f, 0, zoomMax[i], title=temper[i]+" K", points=1000, color = colors[1], penWidth=2))
	
	""" The Keys """
	label.append(JaText(temper[i]+ " K", 0.2, labPosY[i], "NDC", font=legendFont, color=colors[0]))

	

""" Create the Plotter"""
mainPlotter = HPlotJa("Average Events", 600, 1000, 2, 3, True, antiAlias=False)
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
	
mainPlotter.cd(1,1)	
mainPlotter.setPad(1, 1, 0.125, 0.075, 0.75, 0.26)
mainPlotter.setRange(-0.0, funcSup[0], 0.0001, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(2,1)	
mainPlotter.setPad(2, 1, 0.6, 0.1, 0.225, 0.125)
#mainPlotter.setRange(-2.0, zoomMax, 0.01, 1.0)
mainPlotter.setRange(0, zoomMax[0], 0.1, 1.0)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(1,2)	
mainPlotter.setPad(1, 2, 0.125, 0.37, 0.75, 0.26)
mainPlotter.setRange(0.0, funcSup[1], 0.00001, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(2,2)	
mainPlotter.setPad(2, 2, 0.6, 0.395, 0.225, 0.125)
#mainPlotter.setRange(-2.0, zoomMax, 0.01, 1.0)
mainPlotter.setRange(0, zoomMax[1], 0.1, 1.0)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(1,3)	
mainPlotter.setPad(1, 3, 0.125, 0.665, 0.75, 0.26)
#mainPlotter.setRange(0, funcSup[2], 0.0001, 1)
mainPlotter.setRange(0.0, funcSup[2], 0.0001, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = "Time (&micro;s)"
mainPlotter.nameY = "Photons per Unit Time (a.u.)"

mainPlotter.cd(2,3)	
mainPlotter.setPad(2, 3, 0.6, 0.69, 0.225, 0.125)
#mainPlotter.setRange(-2.0, zoomMax, 0.01, 1.0)
mainPlotter.setRange(0, zoomMax[2], 0.1, 1.0)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""



""" Plots all the JHep objects"""
mainPlotter.cd(1,1)
mainPlotter.draw(avEvt[0])
mainPlotter.draw(func[0])
mainPlotter.draw(label[0])
theAxes_11 = mainPlotter.getPad()
theAxes_11.setLabelSpace(0, 0.0)
theAxes_11.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_11)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_11)

mainPlotter.cd(2,1)
mainPlotter.draw(avEvt[0])
mainPlotter.draw(funcZoom[0])
theAxes_21 = mainPlotter.getPad()
theAxes_21.setLabelSpace(0, 0.0)
theAxes_21.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_21)
#theAxes_13.setTicksNumber(0,theAxes_13.getTicksNumber(0)/2)
theAxes_21.setSubTicksNumber(0,2)
#theAxes_13.setSubTicksNumber(1,0)
theAxes_21.setSubTicksSize(0, theAxes_21.getSubTicksSize(0) *2 / 3)
theAxes_21.setSubTicksSize(1, theAxes_21.getSubTicksSize(1) *2 / 3)
theAxes_21.setTicksSize(0, theAxes_21.getTicksSize(0) *2 / 3)
theAxes_21.setTicksSize(1, theAxes_21.getTicksSize(1) *2 / 3)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_21) 

mainPlotter.cd(1,2)
mainPlotter.draw(avEvt[1])
mainPlotter.draw(func[1])
mainPlotter.draw(label[1])
theAxes_12 = mainPlotter.getPad()
theAxes_12.setLabelSpace(0, 0.0)
theAxes_12.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_12)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_12)

mainPlotter.cd(2,2)
mainPlotter.draw(avEvt[1])
mainPlotter.draw(funcZoom[1])
theAxes_22 = mainPlotter.getPad()
theAxes_22.setLabelSpace(0, 0.0)
theAxes_22.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_22)
#theAxes_13.setTicksNumber(0,theAxes_13.getTicksNumber(0)/2)
theAxes_22.setSubTicksNumber(0,2)
#theAxes_13.setSubTicksNumber(1,0)
theAxes_22.setSubTicksSize(0, theAxes_22.getSubTicksSize(0) *2 / 3)
theAxes_22.setSubTicksSize(1, theAxes_22.getSubTicksSize(1) *2 / 3)
theAxes_22.setTicksSize(0, theAxes_22.getTicksSize(0) *2 / 3)
theAxes_22.setTicksSize(1, theAxes_22.getTicksSize(1) *2 / 3)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_22) 


mainPlotter.cd(1,3)
mainPlotter.draw(avEvt[2])
mainPlotter.draw(func[2])
mainPlotter.draw(label[2])
theAxes_13 = mainPlotter.getPad()
theAxes_13.setLabelSpace(0, 0.0)
theAxes_13.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_13)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_13)

mainPlotter.cd(2,3)
mainPlotter.draw(avEvt[2])
mainPlotter.draw(funcZoom[2])
theAxes_23 = mainPlotter.getPad()
theAxes_23.setLabelSpace(0, 0.0)
theAxes_23.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_23)
#theAxes_13.setTicksNumber(0,theAxes_13.getTicksNumber(0)/2)
theAxes_23.setSubTicksNumber(0,2)
#theAxes_13.setSubTicksNumber(1,0)
theAxes_23.setSubTicksSize(0, theAxes_23.getSubTicksSize(0) *2 / 3)
theAxes_23.setSubTicksSize(1, theAxes_23.getSubTicksSize(1) *2 / 3)
theAxes_23.setTicksSize(0, theAxes_23.getTicksSize(0) *2 / 3)
theAxes_23.setTicksSize(1, theAxes_23.getTicksSize(1) *2 / 3)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_23) 

""" Modifies the JaAxes and the axes labels"""
labX = mainPlotter.getLabelX()[0][2]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, mainPlotter, 0.025)
labY = mainPlotter.getLabelY()[0][2]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, mainPlotter, 0.015)

globTitle = JaText()
globTitle.text = "Average Pulses in BGO"
globTitle.font = titleFont
mainPlotter.draw(globTitle)
HPlotJaUtil.setLabelLocationAtCenter(0, globTitle, mainPlotter, 0.955)


mainPlotter.update()

""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = "AvEvtMultiQV2" + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()
print("That's all folks !")

